<?php
class EditBookingVipForm extends Form{
	private $booking_vip = array();
	
	function EditBookingVipForm(){
		Form::Form('EditBookingVipForm');
		
		CGlobal::$website_title = "Sửa booking VIP";
		
		$this->link_css('style/aznet/ui.datepicker.css');	
		$this->link_js('javascript/jquery/ui.datepicker.js');
		
		$id = (int)Url::get("id",0);
		
		if($id){
			$this->booking_vip = DB::select("bookings_vip","id=$id");
		}
		
		if(!$this->booking_vip || $this->booking_vip['status'] == -1 || $this->booking_vip['status'] == 1 || ($this->booking_vip['user_add'] != User::user_name() && !User::is_root() && !User::is_admin()))
		{
			Url::redirect_current();
		}
	}

	function on_submit()
	{
		AZLib::getCats();
		
		$item_id   		= (int)Url::get("item_id",0);
		$time_s			= Url::get("time_start",'');
		$time_e			= Url::get("time_end",'');
		$category   	= (int)Url::get("category",0);
		$num_contract  	= Url::get("num_contract",'');
		
        $item = Item::get_item($this->booking_vip['item_id']);

		if(!$this->errNum)
		{
			$time_start = 0;
			if($time_s!=''){
				$arr = explode('-', $time_s);
				
				if(count($arr) == 3){
					$time_start = mktime(0,0,0,$arr[1],$arr[0],$arr[2]); 
				}
			}
			
			if(!$time_start){
				$time_start = mktime(0,0,0,date('m',TIME_NOW),date('d',TIME_NOW),date('Y',TIME_NOW)); 
			}
			
			$time_end = 0;
			if($time_e!=''){
				$arr = explode('-', $time_e);
				
				if(count($arr) == 3){
					$time_end = mktime(23,59,59,$arr[1],$arr[0],$arr[2]); 
				}
			}
			
			if(!$time_end){
				$this->setFormError('time_end','Bạn chưa nhập vào thời gian kết thúc!');
				return ;
			}
			
			if($time_end < $time_start)
			{
				$this->setFormError('time_end','Thời gian kết thúc phải lớn hơn thời gian bắt đầu!');
				return ;
			}
			
			$item = Item::get_item($item_id);
			// xac dinh xem co hien thi o danh muc cha khong
			$top_cat = 0;
			$cat_id = 0;

			if($category == 1)
			{
				$top_cat = $item['level_1_catid'];
			}else if($category == 2){
                $cat_id = $item['category_id'];
            }
			
			$id = DB::update("bookings_vip",
										array(
												'item_id' 		=> $item_id,
												'cat_id' 		=> $cat_id,
												'top_cat'       => $top_cat,
												'note'			=> AZLib::getParam('note'),
												'time_start'	=> $time_start,
												'time_end' 		=> $time_end,
												'time_edit' 	=> TIME_NOW,
												'user_edit' 	=> User::user_name(),
												'num_contract' 	=> $num_contract
												),"id=".$this->booking_vip['id']);
			
			Url::redirect_current();
		}
	}
	
	function draw(){
		global $display;
		AZLib::getCats();
		$this->beginForm();
		
		$item = Item::get_item($this->booking_vip['item_id']);
		
		if(!$item || ($item && $item['status']==-1)){
			$this->setFormError("item_id","Tin không tồn tại!");
		}
		else{
			$display->add('item_name',stripslashes($item['name']));
			$display->add('item_link',Url::build('item_detail',array('id'=>$item['id'],'ebname'=>AZLib::safe_title($item['name']))));
		}
		
		$top_catid      = $item['level_1_catid'];
		$cat_id         = $item['category_id'];
		
		$display->add('msg',$this->showFormErrorMessages(1));
		
		$display->add('item_id',$this->booking_vip['item_id']);

        if($this->booking_vip['top_cat']){
            $display->add('category',1);
        }else if($this->booking_vip['cat_id']){
            $display->add('category', 2);
        }
		$display->add('num_contract',$this->booking_vip['num_contract']);
		$display->add('top_cat_name',CGlobal::$allCategories[$top_catid]['name']);
		$display->add('cat_name',CGlobal::$allCategories[$cat_id]['name']);
		
		$display->add('note', 	 stripslashes(Url::get("note",$this->booking_vip['note'])));
		
		$display->add('time_start',	Url::get("time_start",date('d-m-Y',$this->booking_vip['time_start'])));
		
		if($this->booking_vip['time_end']){
			$display->add('time_end',	Url::get("time_end",date('d-m-Y',$this->booking_vip['time_end'])));
		}
		else{
			$display->add('time_end',	Url::get("time_end",''));
		}
		
		$display->output('EditBookingVip');
		$this->endForm();
	}
}
?>